package com.example.final_project.Usr.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.final_project.R;
import com.example.final_project.Usr.entry.UserStore;

import java.util.List;

public class ShopInAdapter extends RecyclerView.Adapter {

    private UserStore userStores;
    private Context context;
    public static final int IMG = 0;
    public static final int PRICE = 1;
    public static final int NAME = 2;

    //构造方法
    public ShopInAdapter(UserStore userStores1,Context context1){
        this.userStores = userStores1;
        this.context = context1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case IMG:
                View Inimg = LayoutInflater.from(context).inflate(R.layout.home_shop_in_img,parent,false);
                holder = new ImgHolder(Inimg);
                break;
            case PRICE:
                View Inprice = LayoutInflater.from(context).inflate(R.layout.home_shop_in_price,parent,false);
                holder = new PriceHolder(Inprice);
                break;
            case NAME:
                View Inname = LayoutInflater.from(context).inflate(R.layout.home_shop_in_name,parent,false);
                holder = new NameHolder(Inname);
                break;
        }

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        switch (type){
            case IMG:
                ImgHolder imgHolder = (ImgHolder) holder;
                imgHolder.setImgData();
                break;
            case PRICE:
                PriceHolder priceHolder = (PriceHolder) holder;
                priceHolder.setPriceData();
                break;
            case NAME:
                NameHolder nameHolder = (NameHolder) holder;
                nameHolder.setNameData();
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {

        int type = 0;
        switch (position){
            case IMG:
                type = IMG;
                break;
            case PRICE:
                type = PRICE;
                break;
            case NAME:
                type = NAME;
                break;
        }

        return type;
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    //图片板块Holder
    class ImgHolder extends RecyclerView.ViewHolder {
        public ImgHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setImgData(){
            ImageView imageView = itemView.findViewById(R.id.home_shop_in_img);
            Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+userStores.getImg()).into(imageView);
        }
    }

    //价格板块Holder
    class PriceHolder extends RecyclerView.ViewHolder {
        public PriceHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setPriceData(){
            TextView textView = itemView.findViewById(R.id.home_shop_in_price);
            textView.setText(" ¥ "+userStores.getPrice());
        }

    }

    //名字板块Holder
    class NameHolder extends RecyclerView.ViewHolder {
        public NameHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setNameData(){
            TextView textView = itemView.findViewById(R.id.home_shop_in_name);
            textView.setText(userStores.getName());
        }

    }

}
